package com.gxhmplan.user.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxhmplan.news.dao.NewsDao;
import com.gxhmplan.news.entity.News;
import com.gxhmplan.news.service.NewsService;
import com.gxhmplan.talent.dao.TalentDao;
import com.gxhmplan.user.dao.UserDao;
import com.gxhmplan.user.service.NameException;
import com.gxhmplan.user.service.PasswordException;
import com.gxhmplan.user.service.UserService;
import com.gxhmplan.util.JsonResult;

@Controller
@RequestMapping("")
public class UserController {
	@Resource
	UserService userService;
	@Resource
	NewsService newsService;
	@Resource
	NewsDao newsDao;
	@Resource
	TalentDao talentDao;
	
	@RequestMapping("/pageIndex.htm")
	public ModelAndView index() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/WEB-INF/pages/index/pageIndex.jsp");
		return mav;
	}
	@RequestMapping("/login.htm")
	public ModelAndView login() {
		return new ModelAndView("/WEB-INF/pages/login/login.jsp");
	}
	@ResponseBody
	@RequestMapping("/toLogin.do")
	public JsonResult toLogin(String userName,String password) {
		return new JsonResult(userService.login(userName, password));
	}
	@RequestMapping("/password.htm")
	public ModelAndView password() {
		return new ModelAndView("/WEB-INF/pages/login/password.jsp");
	}
	@RequestMapping("/index.htm")
	public ModelAndView showIndex() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/index.jsp");
		mav.addObject("webSet", userService.findWebSet());
		mav.addObject("banner", userService.findBanner());
		List<Map<String,Object>> newsList = newsService.findNews(1);
		List<Map<String,Object>> newsList2 = newsService.findNews(2);
		List<Map<String,Object>> newsList3 = newsService.findNews(3);
		mav.addObject("newsList", newsList);
		mav.addObject("newsList2", newsList2);
		mav.addObject("newsList3", newsList3);
		return mav;
	}
	@ResponseBody
	@RequestMapping("/newsSearch.do")
	public JsonResult newsSearch(String classify,String searchInfo) {
		List<Map<String, Object>> news = newsDao.findNewsBySearchInfo(classify, searchInfo);
		System.out.println(news);
		return new JsonResult(news);
	}
	@RequestMapping("/about.htm")
	public String about() {
		return "/WEB-INF/pages/show/about.jsp";
	}
	@RequestMapping("/business.htm")
	public String business() {
		return "/WEB-INF/pages/show/business.jsp";
	}
	@RequestMapping("/contactwe.htm")
	public ModelAndView contactwe() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/contactwe.jsp");
		return mav;
	}
	@RequestMapping("/webSet.htm")
	public ModelAndView webSet() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/index/webSet.jsp");
		mav.addObject("webSet", userService.findWebSet());
		return mav;
	}
	@RequestMapping("/banner.htm")
	public ModelAndView banner() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/index/banner.jsp");
		mav.addObject("banner", userService.findBanner());
		return mav;
	}
	@RequestMapping("/updateWebSet.htm")
	public ModelAndView updateWebSet(String keyword,String description) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		if (!"".equals(keyword)) {
			map.put("keyword", keyword);
		}
		if (!"".equals(description)) {
			map.put("description", description);
		}
		userService.updateWebSet(map);
		return new ModelAndView("redirect:/webSet.htm");
	}
	@RequestMapping("/updateBanner.htm")
	public ModelAndView updateBanner(String banner1,String banner2,String banner3,String banner4) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		if (!"".equals(banner1)) {
			map.put("banner1", banner1);
		}
		if (!"".equals(banner2)) {
			map.put("banner2", banner2);
		}
		if (!"".equals(banner3)) {
			map.put("banner3", banner3);
		}
		if (!"".equals(banner4)) {
			map.put("banner4", banner4);
		}
		userService.updateBanner(map);
		return new ModelAndView("redirect:/banner.htm");
	}	
	@ResponseBody
	@RequestMapping("/modifyPwd.do") 
	public JsonResult modifyPwd(String userName,String oldPwd,String newPwd,String newPwd2) {
		return new JsonResult(userService.modifyPwd(userName, oldPwd, newPwd,newPwd2));
	}
	@RequestMapping("/features.htm")
	public ModelAndView features() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/features.jsp");
		return mav;
	}
	@RequestMapping("/domain.htm")
	public ModelAndView domain() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/domain.jsp");
		return mav;
	}
	@RequestMapping("/pastoral.htm")
	public ModelAndView pastoral() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/pastoral.jsp");
		return mav;
	}
	@RequestMapping("/toilet.htm")
	public ModelAndView toilet() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/toilet.jsp");
		return mav;
	}
	@RequestMapping("/rural.htm")
	public ModelAndView rural() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/rural.jsp");
		return mav;
	}
	@ExceptionHandler(NameException.class)
	@ResponseBody
	public Object nameexp(NameException e) {
		e.printStackTrace();
		return new JsonResult(2, e);
	}
	@ExceptionHandler(PasswordException.class)
	@ResponseBody
	public Object pwdexp(PasswordException e) {
		e.printStackTrace();
		return new JsonResult(3, e);
	}
}
